﻿' Copyright (c) 2013  Christoc.com
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
Imports DotNetNuke
Imports DotNetNuke.Services.Exceptions

''' -----------------------------------------------------------------------------
''' <summary>
''' The Settings class manages Module Settings
''' 
''' Typically your settings control would be used to manage settings for your module.
''' There are two types of settings, ModuleSettings, and TabModuleSettings.
''' 
''' ModuleSettings apply to all "copies" of a module on a site, no matter which page the module is on. 
''' 
''' TabModuleSettings apply only to the current module on the current page, if you copy that module to
''' another page the settings are not transferred.
''' 
''' If you happen to save both TabModuleSettings and ModuleSettings, TabModuleSettings overrides ModuleSettings.
''' 
''' Below we have some examples of how to access these settings but you will need to uncomment to use.
''' 
''' Because the control inherits from SiteLinksSettingsBase you have access to any custom properties
''' defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
''' </summary>
''' -----------------------------------------------------------------------------
Public Class Settings
    Inherits SiteLinksSettingsBase

#Region "Base Method Implementations"

    Public Sub LoadStyles()
        Try
            ddlSiteLinksStyleOfLinks.Items.Clear()
            ddlSiteLinksStyleOfLinks.Items.Add("Vertical List")
            ddlSiteLinksStyleOfLinks.Items.Add("Horizontal List")
            ddlSiteLinksStyleOfLinks.Items.Add("Unordered List")
            ddlSiteLinksStyleOfLinks.Items.Add("Ordered List")
        Catch exc As Exception           'Module failed to load
            ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    Public Sub LoadTypes()
        Try
            ddlSiteLinksTypeOfLinks.Items.Clear()
            ddlSiteLinksTypeOfLinks.Items.Add("Same Level")
            ddlSiteLinksTypeOfLinks.Items.Add("Child Level")
            ddlSiteLinksTypeOfLinks.Items.Add("Parent Level")
            ddlSiteLinksTypeOfLinks.Items.Add("Site Root")
        Catch exc As Exception           'Module failed to load
            ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    Public Sub LoadAllPages(Optional ByRef CurrentlySelectedPage As Integer = 0)
        Try
            ddlSiteLinksRootPage.Items.Clear()
            Dim NoPageListItem As New ListItem
            NoPageListItem.Text = "--Current Page--"
            NoPageListItem.Value = -1
            ddlSiteLinksRootPage.Items.Add(NoPageListItem)
            If CurrentlySelectedPage = 0 Then
                Dim SiteDatabase As New SiteLinksController
                Dim SitePages As Classes.SitePages = SiteDatabase.GetAllPages
                SiteDatabase = Nothing
                For Each SitePage As Classes.SitePage In SitePages
                    Dim ddlListItem As New ListItem
                    ddlListItem.Text = SitePage.TabID.ToString + " - " + SitePage.TabName + " - " + SitePage.TabPath.Replace("//", "/")
                    ddlListItem.Value = SitePage.TabID
                    ddlSiteLinksRootPage.Items.Add(ddlListItem)
                Next
                SitePages = Nothing
                ddlSiteLinksRootPage.SelectedIndex = 0
            Else
                Dim SiteDatabase As New SiteLinksController
                Dim SitePages As Classes.SitePages = SiteDatabase.GetAllPages
                SiteDatabase = Nothing
                For Each SitePage As Classes.SitePage In SitePages
                    Dim ddlListItem As New ListItem
                    ddlListItem.Text = SitePage.TabID.ToString + " - " + SitePage.TabName + " - " + SitePage.TabPath.Replace("//", "/")
                    ddlListItem.Value = SitePage.TabID
                    ddlSiteLinksRootPage.Items.Add(ddlListItem)
                    If CurrentlySelectedPage = SitePage.TabID Then
                        ddlSiteLinksRootPage.SelectedIndex = ddlSiteLinksRootPage.Items.Count - 1
                    End If
                Next
                SitePages = Nothing
            End If
        Catch exc As Exception           'Module failed to load
            ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' LoadSettings loads the settings from the Database and displays them
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overrides Sub LoadSettings()
        Try
            If (Page.IsPostBack = False) Then
                LoadStyles()
                LoadTypes()
                If (Settings.Contains("LinksStyle")) Then
                    ddlSiteLinksStyleOfLinks.SelectedIndex = CInt(Settings("LinksStyle"))
                End If
                If (Settings.Contains("LinksType")) Then
                    ddlSiteLinksTypeOfLinks.SelectedIndex = CInt(Settings("LinksType"))
                End If
                If (Settings.Contains("LinksRootPage")) Then
                    LoadAllPages(CInt(Settings("LinksRootPage")))
                Else
                    LoadAllPages()
                End If
                If (Settings.Contains("LinksHeader")) Then
                    txtSiteLinksHeaderText.Text = HttpUtility.HtmlDecode(Settings("LinksHeader"))
                End If
                If (Settings.Contains("LinksFooter")) Then
                    txtSiteLinksFooterText.Text = HttpUtility.HtmlDecode(Settings("LinksFooter"))
                End If
                If (Settings.Contains("LinksCSSClass")) Then
                    txtSiteLinksCSSClass.Text = HttpUtility.HtmlDecode(Settings("LinksCSSClass"))
                End If
                If (Settings.Contains("LinksUseCustomCSS")) Then
                    If Settings("LinksUseCustomCSS") = "true" Then
                        chkSiteLinksUseCustomCSS.Checked = True
                    Else
                        chkSiteLinksUseCustomCSS.Checked = False
                    End If
                End If
                If (Settings.Contains("LinksCustomCSS")) Then
                    txtSiteLinksCustomCSS.Text = HttpUtility.HtmlDecode(Settings("LinksCustomCSS"))
                End If
                If (Settings.Contains("LinksUseTabName")) Then
                    If Settings("LinksUseTabName") = "true" Then
                        chkSiteLinksUseTabName.Checked = True
                    Else
                        chkSiteLinksUseTabName.Checked = False
                    End If
                End If
                If (Settings.Contains("LinksHorizontalSeparator")) Then
                    txtSiteLinksHorizontalLinkSeparator.Text = HttpUtility.HtmlDecode(Settings("LinksHorizontalSeparator"))
                Else
                    txtSiteLinksHorizontalLinkSeparator.Text = "&nbsp;"
                End If
                If (Settings.Contains("LinksVerticalSeparator")) Then
                    txtSiteLinksVerticalLinkSeparator.Text = HttpUtility.HtmlDecode(Settings("LinksVerticalSeparator"))
                Else
                    txtSiteLinksVerticalLinkSeparator.Text = "<br/>"
                End If
            End If
        Catch exc As Exception           'Module failed to load
            ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' UpdateSettings saves the modified settings to the Database
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Overrides Sub UpdateSettings()
        Try
            Dim objModules As New Entities.Modules.ModuleController
            Dim CurrentStyle As Classes.LinksStyle = ddlSiteLinksStyleOfLinks.SelectedIndex
            objModules.UpdateTabModuleSetting(TabModuleId, "LinksStyle", CurrentStyle)
            Dim CurrentType As Classes.LinksType = ddlSiteLinksTypeOfLinks.SelectedIndex
            objModules.UpdateTabModuleSetting(TabModuleId, "LinksType", CurrentType)
            objModules.UpdateTabModuleSetting(TabModuleId, "LinksRootPage", ddlSiteLinksRootPage.SelectedValue)
            objModules.UpdateTabModuleSetting(TabModuleId, "LinksHeader", HttpUtility.HtmlEncode(txtSiteLinksHeaderText.Text))
            objModules.UpdateTabModuleSetting(TabModuleId, "LinksFooter", HttpUtility.HtmlEncode(txtSiteLinksFooterText.Text))
            objModules.UpdateTabModuleSetting(TabModuleId, "LinksCSSClass", HttpUtility.HtmlEncode(txtSiteLinksCSSClass.Text))
            If chkSiteLinksUseCustomCSS.Checked = True Then
                objModules.UpdateTabModuleSetting(TabModuleId, "LinksUseCustomCSS", "true")
            Else
                objModules.UpdateTabModuleSetting(TabModuleId, "LinksUseCustomCSS", "false")
            End If
            objModules.UpdateTabModuleSetting(TabModuleId, "LinksCustomCSS", HttpUtility.HtmlEncode(txtSiteLinksCustomCSS.Text))
            If chkSiteLinksUseTabName.Checked = True Then
                objModules.UpdateTabModuleSetting(TabModuleId, "LinksUseTabName", "true")
            Else
                objModules.UpdateTabModuleSetting(TabModuleId, "LinksUseTabName", "false")
            End If
            objModules.UpdateTabModuleSetting(TabModuleId, "LinksHorizontalSeparator", HttpUtility.HtmlEncode(txtSiteLinksHorizontalLinkSeparator.Text))
            objModules.UpdateTabModuleSetting(TabModuleId, "LinksVerticalSeparator", HttpUtility.HtmlEncode(txtSiteLinksVerticalLinkSeparator.Text))
            MyBase.UpdateSettings()
        Catch exc As Exception           'Module failed to load
            ProcessModuleLoadException(Me, exc)
        End Try
    End Sub

#End Region
End Class